<template>
  <div class="ranking">
    <div class="reveal-overlay" v-if="showRevealAnimation" @animationend="onAnimationEnd">
      <div class="reveal-text" :class="{ 'active': animationStep >= 1 }">金山学院</div>
      <div class="reveal-text second" :class="{ 'active': animationStep >= 2 }">牛人榜</div>
      <div class="reveal-crown" :class="{ 'active': animationStep >= 3 }">👑</div>
      <div class="reveal-flash" :class="{ 'active': animationStep >= 4 }"></div>
    </div>

    <div class="ranking-container" :class="{ 'hidden': showRevealAnimation }">
      <div style="text-align: center; margin-bottom: 20px;">
        <img src="@/assets/emblemAndbadge/系徽4.png" alt="金山学院校徽" class="school-logo-small">
      </div>
      <h1 class="ranking-title">金山学院牛人榜</h1>
      <ul class="ranking-list">
        <li 
          v-for="(student, index) in filteredStudents" 
          :key="student.id"
          class="student-card"
          :class="{ 
            'top-champion': student.level === '最强王者', 
            'master-rank': student.level === '王者',
            'diamond-rank': student.level === '钻石',
            'platinum-rank': student.level === '铂金',
            'gold-rank': student.level === '黄金',
            'appear': revealComplete 
          }"
          :style="{ 'animation-delay': index * 0.1 + 's' }"
          @click="navigateToStudentDetails(student)"
        >
          <div class="student-main-info">
            <span class="rank" :class="`rank-${index + 1}`">{{ index + 1 }}</span>
            <div class="avatar-container">
              <img :src="student.photo" :alt="student.name" class="student-avatar" @error="handleImageError">
              <div class="avatar-glow champion-glow" v-if="student.level === '最强王者'"></div>
              <div class="avatar-glow master-glow" v-else-if="student.level === '王者'"></div>
              <div class="avatar-glow diamond-glow" v-else-if="student.level === '钻石'"></div>
              <div class="avatar-glow platinum-glow" v-else-if="student.level === '铂金'"></div>
              <div class="avatar-glow gold-glow" v-else-if="student.level === '黄金'"></div>
            </div>
            <div class="student-details">
              <div class="student-name">{{ student.name }} <span class="medal" v-if="index < 3">{{ medals[index] }}</span></div>
              <div class="student-desc">{{ student.major }} - {{ student.specialty }}</div>
              <div class="student-level">
                <span class="rank-badge" :class="{
                  'champion-badge': student.level === '最强王者',
                  'master-badge': student.level === '王者',
                  'diamond-badge': student.level === '钻石',
                  'platinum-badge': student.level === '铂金',
                  'gold-badge': student.level === '黄金'
                }">{{ student.level }}</span>
              </div>
            </div>
          </div>
          <div class="student-achievements">{{ student.achievements }}</div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import soundManager from '@/utils/SoundManager';
import defaultAvatar from '@/assets/people_NR/other.jpg';
import { students } from '@/data/students';

export default {
  name: 'RankingView',
  data() {
    return {
      medals: ['👑', '🥈', '🥉'],
      students,
      showRevealAnimation: false,
      revealComplete: false,
      animationStep: 0,
      defaultAvatar
    }
  },
  mounted() {
    // 初始化音效管理器
    soundManager.init();
    
    // 检查是否是首次访问
    this.checkFirstVisit();
    
    // 添加学生卡片动画效果
    if (this.revealComplete) {
      this.animateStudentCards();
    }
  },
  beforeDestroy() {
    // 停止所有音效
    soundManager.stop('drumroll');
    soundManager.stop('reveal');
    soundManager.stop('applause');
  },
  computed: {
    filteredStudents() {
      return this.students;
    }
  },
  methods: {
    // 添加动画学生卡片方法
    animateStudentCards() {
      this.$nextTick(() => {
        const cards = this.$el.querySelectorAll('.student-card');
        cards.forEach((card, index) => {
          setTimeout(() => {
            card.classList.add('appear');
          }, index * 100);
        });
      });
    },
    // 导航到学生详情页面
    navigateToStudentDetails(student) {
      // 播放点击音效
      this.playClickSound();
      
      // 让所有学生都跳转到champion页面
      this.$router.push({ name: 'champion', params: { id: student.id } });
    },
    checkFirstVisit() {
      // 检查本地存储中是否有访问记录
      const hasVisited = localStorage.getItem('hasVisitedRanking');
      
      if (!hasVisited) {
        // 第一次访问，显示动画
        this.showRevealAnimation = true;
        // 开始动画序列
        this.startRevealAnimation();
        // 记录已访问
        localStorage.setItem('hasVisitedRanking', 'true');
      } else {
        // 非首次访问，直接显示排行榜
        this.showRevealAnimation = false;
        this.revealComplete = true;
      }
    },
    startRevealAnimation() {
      // 播放鼓点音效
      soundManager.play('drumroll');
      
      // 动画步骤定时器
      setTimeout(() => { this.animationStep = 1; }, 300);
      setTimeout(() => { this.animationStep = 2; }, 800);
      setTimeout(() => { 
        this.animationStep = 3; 
        // 切换到揭晓音效
        soundManager.stop('drumroll');
        soundManager.play('reveal');
      }, 1500);
      setTimeout(() => { this.animationStep = 4; }, 2000);
    },
    onAnimationEnd(e) {
      // 当闪光动画结束后，显示排行榜
      if (e.target.classList.contains('reveal-flash') && this.animationStep >= 4) {
        this.showRevealAnimation = false;
        this.revealComplete = true;
        // 播放掌声音效
        soundManager.play('applause');
        
        // 添加延迟以确保DOM已更新后再动画
        setTimeout(() => {
          this.animateStudentCards();
        }, 100);
      }
    },
    playClickSound() {
      soundManager.play('click');
    },
    handleImageError(e) {
      // 图片加载失败时替换为默认头像
      e.target.src = this.defaultAvatar;
    }
  }
}
</script>

<style scoped>
body {
  margin: 0;
  padding: 0;
  font-family: 'Microsoft YaHei', sans-serif;
  background: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
}

.ranking {
  padding: 20px;
  background: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
  min-height: 100vh;
  position: relative;
  overflow: hidden;
}

/* 揭晓动画样式 */
.reveal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: #000;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
}

.reveal-text {
  font-size: 0;
  font-weight: bold;
  opacity: 0;
  transform: scale(0.5);
  transition: all 0.8s cubic-bezier(0.17, 0.67, 0.83, 0.67);
}

.reveal-text.active {
  font-size: 5em;
  opacity: 1;
  transform: scale(1);
}

.reveal-text.second {
  color: #ffd700;
}

.reveal-crown {
  font-size: 0;
  opacity: 0;
  transform: translateY(-50px) rotate(-30deg);
  transition: all 0.5s ease;
  margin-top: 20px;
}

.reveal-crown.active {
  font-size: 8em;
  opacity: 1;
  transform: translateY(0) rotate(0);
  animation: bounce 0.5s ease 0.5s;
}

.reveal-flash {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: transparent;
  pointer-events: none;
  opacity: 0;
}

.reveal-flash.active {
  animation: flash 0.8s ease-out forwards;
}

@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-20px); }
}

@keyframes flash {
  0% { 
    opacity: 0;
    background-color: transparent;
  }
  30% { 
    opacity: 1;
    background-color: white;
  }
  100% { 
    opacity: 0;
    background-color: transparent;
  }
}

.ranking-container {
  max-width: 800px;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 15px;
  padding: 20px;
  box-shadow: 0 5px 15px rgba(0,0,0,0.1);
  transition: opacity 0.5s ease;
}

.ranking-container.hidden {
  opacity: 0;
}

.school-logo-small {
  width: 80px;
  height: 80px;
}

.ranking-title {
  text-align: center;
  color: #3a6ea5;
  margin-bottom: 30px;
  position: relative;
}

.ranking-title:after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 100px;
  height: 3px;
  background: linear-gradient(90deg, transparent, #3a6ea5, transparent);
}

.ranking-list {
  list-style: none;
  padding: 0;
}

.student-card {
  position: relative;
  padding: 15px;
  margin-bottom: 15px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease, opacity 0.5s ease;
  text-decoration: none;
  color: inherit;
  opacity: 0;
  transform: translateY(20px);
  cursor: pointer;
  overflow: hidden;
}

.student-card.appear {
  opacity: 1;
  transform: translateY(0);
}

.student-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(0,0,0,0.15);
}

.student-card.top-champion {
  background: linear-gradient(to right, white, #fffef0);
  border-left: 4px solid gold;
}

.student-card.master-rank {
  background: linear-gradient(to right, white, #f8f0ff);
  border-left: 4px solid #9d4edd;
}

.student-card.diamond-rank {
  background: linear-gradient(to right, white, #f0fbff);
  border-left: 4px solid #00b4d8;
}

.student-card.platinum-rank {
  background: linear-gradient(to right, white, #f0f8ff);
  border-left: 4px solid #457b9d;
}

.student-card.gold-rank {
  background: linear-gradient(to right, white, #fffbf0);
  border-left: 4px solid #faa307;
}

.student-main-info {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.avatar-container {
  position: relative;
  margin-right: 15px;
}

.rank {
  font-size: 1.8em;
  font-weight: bold;
  color: #3a6ea5;
  width: 40px;
  text-align: center;
  margin-right: 15px;
}

.rank-1 { 
  color: gold; 
  text-shadow: 0 0 5px rgba(255, 215, 0, 0.5);
  animation: pulse 2s infinite;
}
.rank-2 { color: silver; }
.rank-3 { color: #cd7f32; }

@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.1); }
}

.student-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  object-fit: cover;
  border: 3px solid #eee;
  box-shadow: 0 2px 5px rgba(0,0,0,0.1);
  transition: transform 0.3s ease;
}

.student-card:hover .student-avatar {
  transform: scale(1.05);
}

.student-card.top-champion .student-avatar {
  border: 3px solid gold;
  box-shadow: 0 0 10px rgba(255, 215, 0, 0.5);
}

.student-card.master-rank .student-avatar {
  border: 3px solid #9d4edd;
  box-shadow: 0 0 10px rgba(157, 78, 221, 0.5);
}

.student-card.diamond-rank .student-avatar {
  border: 3px solid #00b4d8;
  box-shadow: 0 0 10px rgba(0, 180, 216, 0.5);
}

.student-card.platinum-rank .student-avatar {
  border: 3px solid #457b9d;
  box-shadow: 0 0 10px rgba(69, 123, 157, 0.5);
}

.student-card.gold-rank .student-avatar {
  border: 3px solid #faa307;
  box-shadow: 0 0 10px rgba(250, 163, 7, 0.5);
}

.avatar-glow {
  position: absolute;
  top: -5px;
  left: -5px;
  right: -5px;
  bottom: -5px;
  border-radius: 50%;
  background: transparent;
  border: 2px solid;
  animation: glow 2s infinite alternate;
  pointer-events: none;
}

.champion-glow {
  border-color: gold;
  box-shadow: 0 0 10px gold;
}

.master-glow {
  border-color: #9d4edd;
  box-shadow: 0 0 10px #9d4edd;
}

.diamond-glow {
  border-color: #00b4d8;
  box-shadow: 0 0 10px #00b4d8;
}

.platinum-glow {
  border-color: #457b9d;
  box-shadow: 0 0 10px #457b9d;
}

.gold-glow {
  border-color: #faa307;
  box-shadow: 0 0 10px #faa307;
}

@keyframes glow {
  0% { opacity: 0.6; transform: scale(1); }
  100% { opacity: 1; transform: scale(1.1); }
}

.student-details {
  flex-grow: 1;
}

.student-name {
  font-weight: bold;
  font-size: 1.3em;
  display: flex;
  align-items: center;
}

.medal {
  font-size: 1.2em;
  margin-left: 8px;
}

.student-desc {
  color: #555;
  font-size: 0.95em;
  margin-top: 4px;
}

.student-level {
  margin-top: 8px;
}

.student-achievements {
  color: #e63946;
  font-size: 0.9em;
  padding-top: 8px;
  border-top: 1px solid #eee;
}

.rank-badge {
  background: #e9ecef;
  padding: 5px 10px;
  border-radius: 20px;
  font-size: 0.85em;
  color: #495057;
  font-weight: bold;
  white-space: nowrap;
}

.champion-badge {
  background: linear-gradient(45deg, #ffd700, #ffec8b);
  color: #8b4513;
  box-shadow: 0 0 10px rgba(255, 215, 0, 0.5);
  animation: glowing 2s infinite alternate;
}

.master-badge {
  background: linear-gradient(45deg, #9d4edd, #c77dff);
  color: white;
  box-shadow: 0 0 10px rgba(157, 78, 221, 0.5);
  animation: glowing 2s infinite alternate;
}

.diamond-badge {
  background: linear-gradient(45deg, #00b4d8, #90e0ef);
  color: #023e8a;
  box-shadow: 0 0 10px rgba(0, 180, 216, 0.5);
  animation: glowing 2s infinite alternate;
}

.platinum-badge {
  background: linear-gradient(45deg, #457b9d, #a8dadc);
  color: #1d3557;
  box-shadow: 0 0 10px rgba(69, 123, 157, 0.5);
  animation: glowing 2s infinite alternate;
}

.gold-badge {
  background: linear-gradient(45deg, #faa307, #ffba08);
  color: #7f4f24;
  box-shadow: 0 0 10px rgba(250, 163, 7, 0.5);
  animation: glowing 2s infinite alternate;
}

@keyframes glowing {
  0% { box-shadow: 0 0 5px rgba(255, 215, 0, 0.5); }
  100% { box-shadow: 0 0 15px rgba(255, 215, 0, 0.8), 0 0 20px rgba(255, 215, 0, 0.5); }
}

@media (max-width: 768px) {
  .reveal-text.active {
    font-size: 3em;
  }
  
  .reveal-crown.active {
    font-size: 5em;
  }

  .student-card {
    padding: 15px 10px;
  }
  
  .rank {
    font-size: 1.5em;
    width: 30px;
    margin-right: 10px;
  }
  
  .student-avatar {
    width: 50px;
    height: 50px;
  }

  .student-name {
    font-size: 1.1em;
  }
  
  .student-desc {
    font-size: 0.85em;
  }
  
  .rank-badge {
    font-size: 0.75em;
    padding: 3px 8px;
  }
}
</style> 